<template>
  <div>
    <el-card>
      <div class="dingbu">
        <p>
          用户名称:<el-input v-model="input" placeholder="请输入内容" clearable @input="search" @clear="clear" />
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add">新增</el-button>
        </p>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          type="index"
        />
        <el-table-column
          label="用户名"
          prop="username"
        />
        <el-table-column
          label="身份"
          prop="role"
        />
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button @click="upd(scope.row)"><a class="blue">编辑</a></el-button>
            <el-button @click="del(scope.row)"><a class="red">删除</a></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="t"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog title="新增用户" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="账号名称"   prop="username">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="账号密码"   prop="password">
          <el-input v-model="form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="用户身份"   prop="role">
          <el-select v-model="form.role" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addsave">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑用户" :visible.sync="dialogFormVisible2" width="30%">
      <el-form :model="form2" :rules="rules" ref="ruleForm">
        <el-form-item label="账号名称"   prop="username">
          <el-input v-model="form2.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="账号密码"   prop="password">
          <el-input v-model="form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="用户身份"   prop="role">
          <el-select v-model="form2.role" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="updsave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      input:'',
      size: 5,
      page: 1,
      t: 0,
      options: [{
        value: '管理员',
        label: '管理员'
      }, {
        value: '普通用户',
        label: '普通用户'
      }],
      form: {
        username: '',
        role: '',
        password:''
      },
      form2: {
        id: '',
        username: '',
        password:'',
        role: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号名称', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入账号密码', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请输入用户身份', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getuser()
  },
  mounted() {},
  methods: {
    async getuser() {
      var res = await this.http.get('/user/user/',{
        params:{
            page: this.page,
            size: this.size
        }
      })
      console.log(res)
      if (res.data.code == 200) {
        this.tableData = res.data.data
        this.t = res.data.total
      }
    },
    async search() {
      if (!this.pageisornot) {
        this.page = 1
      }
      if (this.input != '') {
        var res = await this.http.get('/user/user/', {
          params: { username: this.input, page: this.page,
            size: this.size }
        })
        if (res.data.code == 200) {
          this.tableData = res.data.data
          this.t = res.data.total
        }
      } else {
        this.getuser()
      }
      this.pageisornot = true
    },
    clear() {
      console.log(555555555)
      this.getuser()
    },
    add() {
      this.dialogFormVisible = true
    },
    async addsave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          var res = await this.http.post('/user/user/', {
            username: this.form.username,
            role: this.form.role,
            password:this.form.password
          })
          if (res.data.code == 200) {
            this.dialogFormVisible = false
            this.getuser()
            this.$message.success('添加成功')
          }
        }
      })
    },
    upd(row) {
      this.dialogFormVisible2 = true
      this.form2.id = row.id
      this.form2.username = row.username
      this.form2.role = row.role
      this.form2.role = row.password
    },
    async updsave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          var res = await this.http.put(`/user/user/${this.form2.id}`, {
            username: this.form2.username,
            role: this.form2.role,
            password:this.form2.password
          })
          if (res.data.code == 200) {
            this.dialogFormVisible2 = false
            this.getuser()
            this.$message.success('修改成功')
          }
        }
      })
    },
    handleSizeChange(v) {
      this.size = v
      this.getuser()
    },
    handleCurrentChange(v) {
      this.page = v
      this.getuser()
    },
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        var res = await this.http.delete(`/user/user/${row.id}`)
        if (res.data.code == 200) {
          this.getuser()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
          this.$message({
            type: 'error',
            message: '没有权限!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style scoped>
    .el-input{
      width: 200px;
      margin-left: 7px;
    }
     .dingbu{
      padding-bottom: 15px;
      border-bottom: 2px solid ghostwhite;
    }
    .dingbu p .el-button{
      float: right;
      margin-right: 10px;
    }
    .blue{
      color: #3175fb;
    }
    .red{
      color: red;
    }
    .el-table .el-button{
      background: 0;
      padding-left: 0;
      padding-right: 0;
      display: inline-block;
      border:0;
    }
    .el-pagination{
      margin-top: 20px;
      text-align: center;
    }
</style>
